So, when you build a type using the C# delegate keyword, you indirectly declare a class type that derives from System.MulticastDelegate. This class provides descendents with access to a list that contains the addresses of the methods maintained by the delegate object, as well as several additional methods (and a few overloaded operators) to interact with the invocation list. Here are some select members of System.MulticastDelegate:
public abstract class MulticastDelegate : Delegate { // Returns the list of methods "pointed to." public sealed override Delegate[] GetInvocationList(); // Overloaded operators. public static bool operator ==(MulticastDelegate d1, MulticastDelegate d2); public static bool operator !=(MulticastDelegate d1, MulticastDelegate d2); // Used internally to manage the list of methods maintained by the delegate. private IntPtr _invocationCount; private object _invocationList; }
System.MulticastDelegate obtains additional functionality from its parent class, System.Delegate. Here is a partial snapshot of the class definition:
public abstract class Delegate : ICloneable, ISerializable { // Methods to interact with the list of functions. public static Delegate Combine(params Delegate[] delegates); public static Delegate Combine(Delegate a, Delegate b); public static Delegate Remove(Delegate source, Delegate value); public static Delegate RemoveAll(Delegate source, Delegate value); // Overloaded operators. public static bool operator ==(Delegate d1, Delegate d2); public static bool operator !=(Delegate d1, Delegate d2); // Properties that expose the delegate target. public MethodInfo Method { get; } public object Target { get; } }
Now, understand that you can never directly derive from these base classes in your code (it is a compiler error to do so). Nevertheless, when you use the delegate keyword, you have indirectly created a class that “is-a” MulticastDelegate. Table 11-1 documents the core members common to all delegate types.
Table 11-1. Select Members of System.MultcastDelegate/System.Delegate
Member | Meaning in Life |
---|---|
Method | This property returns a System.Reflection.MethodInfo object that represents details of a static method maintained by the delegate. |
Target | If the method to be called is defined at the object level (rather than a static method), Target returns an object that represents the method maintained by the delegate. If the value returned from Target equals null, the method to be called is a static member. |
Combine() | This static method adds a method to the list maintained by the delegate. In C#, you trigger this method using the overloaded += operator as a shorthand notation. |
GetInvocationList() | This method returns an array of System.Delegate objects, each representing a particular method that may be invoked. |
Remove() RemoveAll() | These static methods remove a method (or all methods) from the delegate’s invocation list. In C#, the Remove() method can be called indirectly using the overloaded -= operator. |